import org.json.simple.JSONObject;                                                                                      
import org.omg.Messaging.SyncScopeHelper;                                                                               
                                                                                                                        
import java.io.IOException;                                                                                             
import java.io.ObjectInputStream;                                                                                       
import java.io.ObjectOutputStream;                                                                                      
import java.net.ServerSocket;                                                                                           
import java.net.Socket;                                                                                                 
import java.util.Scanner;                                                                                               
                                                                                                                        
                                                                                                                        
public class Server {                                                                                                   
    public static void main(String[] args){                                                                             
        try{                                                                                                            
            System.out.println("服务器开始运行...");                                                                           
            ServerSocket serverSocket = new ServerSocket(9999);                                                         
            while(true){                                                                                                
                Socket socket = serverSocket.accept();   //接受客户的请求                                                      
                new Thread (new Server_listen(socket)).start();  //监听线程                                                 
                new Thread (new Server_send(socket)).start();  //发送线程                                                   
            }                                                                                                           
        }catch(Exception e){                                                                                            
            e.printStackTrace();                                                                                        
        }                                                                                                               
    }                                                                                                                   
}                                                                                                                       
                                                                                                                        
//定义监听类                                                                                                                 
class Server_listen implements Runnable{                                                                                
    private Socket socket;                                                                                              
                                                                                                                        
    Server_listen(Socket socket){                                                                                       
        this.socket = socket;                                                                                           
    }                                                                                                                   
                                                                                                                        
    @Override                                                                                                           
    public void run() {                                                                                                 
        try{                                                                                                            
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());  //输入数据流                            
            while(true) {                                                                                               
                System.out.println(ois.readObject());  //打印客户端传来的Object                                                 
            }                                                                                                           
        }catch(Exception e){                                                                                            
            e.printStackTrace();                                                                                        
        }finally {                                                                                                      
            try{                                                                                                        
                socket.close();                                                                                         
            }catch(Exception e){                                                                                        
                e.printStackTrace();                                                                                    
            }                                                                                                           
        }                                                                                                               
    }                                                                                                                   
}                                                                                                                       
                                                                                                                        
//服务器发送数据类                                                                                                              
class Server_send implements Runnable{                                                                                  
    private Socket socket;                                                                                              
                                                                                                                        
    Server_send(Socket socket){                                                                                         
        this.socket = socket;                                                                                           
    }                                                                                                                   
                                                                                                                        
    @Override                                                                                                           
    public void run() {                                                                                                 
        try {                                                                                                           
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());  //对象输出流                         
            Scanner scanner = new Scanner(System.in);                                                                   
            while (true) {                                                                                              
                System.out.println("请输入内容:");                                                                           
                System.out.println("请输入要发送的内容:");                                                                       
                String string = new String(scanner.nextLine());                                                         
                //从外部导入JSON_SIMPLE包并使用                                                                                  
                JSONObject object = new JSONObject();                                                                   
                object.put("type","chat");                                                                              
                object.put("msg",string);                                                                               
                oos.writeObject("服务器:"+object.get("msg"));                                                              
                oos.writeObject(object.get("msg"));                                                                     
                oos.flush();                                                                                            
            }                                                                                                           
        } catch (Exception e) {                                                                                         
            e.printStackTrace();                                                                                        
        }                                                                                                               
    }                                                                                                                   
}                                                                                                                       